<template>
	<view :style="{height:PhoneInfo.windowHeight + 'px'}">
		<mvBar :mysNavConfig="mysNavConfig" @onLeftClick="onLeftClick"></mvBar>
		<u-toast ref="uToast"></u-toast>
		<view class="uniview" style="margin-bottom: 0px;">
			<uni-card padding="10px 0">
				<view class="title">
					<view class="text" @click="pointtrend(item)">
						全部选中
					</view>
					<view class="switch">
						<selectSwitch :defaultSwitch="true" :checked_bj_color="checkedbj" @change="changeallswitch" />
					</view>
				</view>
			</uni-card>
		</view>
		<view style="height: 70%; overflow: hidden; padding-top: 10px;">
			<scroll-view scroll-y="true" style="height: 100%;">
				<view class="uniview" style="margin-top: 5px;" v-for="(item,index) in indexinfo"
					:key="item.pointFeatureRealId">
					<uni-card padding="10px 0">
						<template v-slot:title>
							<view class="title">
								<view class="text" @click="pointtrend(item)">
									{{item.name}}{{item.unit==''?'(无)':item.unit=='ma'?'(%)':'('+item.unit+')'}}
								</view>
								<view class="switch">
									<selectSwitch :checked_bj_color="item.enableStatus==1?'#18bc37':'#fa3534'"
										:defaultSwitch="item.enableStatus==1?true:false"
										@change="switchchange(item.pointFeatureRealId)" />
								</view>
							</view>
						</template>
						<view style="padding: 0px 10px;"
							v-if="item.code=='XAN'||item.code=='YAN'||item.code=='ZAN'||item.code=='POWER'">
							<u-form labelPosition=" left" :model="item">
								<view class="inputviews">
									<view class="inputview">
										<u-input type="digit" inputAlign='center' v-model="item.yj" placeholder="下限值"
											border="bottom">
										</u-input>
									</view>
									<view class="texts">{{'<'}} </view>
									<view class="inpvie">一级报警</view>
									<view class="texts"> {{'<'}}</view>
									<view class="inputview">
										<u-input type="digit" inputAlign='center' v-model="item.unusual"
											placeholder="上限值" border="bottom">
										</u-input>
									</view>
								</view>
								<view class="inputviews">
									<view class="inputview">
										<u-input type="digit" inputAlign='center' v-model="item.bj" placeholder="下限值"
											border="bottom">
										</u-input>
									</view>
									<view class="texts">{{'<'}} </view>
									<view class="inpvie">二级报警</view>
									<view class="texts"> {{'<'}}</view>
									<view class="inputview">
										<u-input type="digit" inputAlign='center' v-model="item.warning"
											placeholder="上限值" border="bottom">
										</u-input>
									</view>
								</view>
								<view class="inputviews">
									<view class="inputview">
										<u-input type="digit" inputAlign='center' v-model="item.gb" placeholder="下限值"
											border="bottom">
										</u-input>
									</view>
									<view class="texts">{{'<'}} </view>
									<view class="inpvie">三级报警</view>
									<view class="texts"> {{'<'}}</view>
									<view class="inputview">
										<u-input type="digit" inputAlign='center' v-model="item.highRisk"
											placeholder="上限值" border="bottom">
										</u-input>
									</view>
								</view>
							</u-form>
						</view>
						<view style="padding: 0px 10px;" v-else>
							<u-form labelPosition="left" :model="item">
								<u-form-item label="一级报警" label-width="75" prop="unusual" borderBottom>
									<u-input type="digit" v-model="item.unusual" placeholder="请输入一级报警值" border="none">
									</u-input>
								</u-form-item>
								<u-form-item label="二级报警" label-width="75" prop="warning" borderBottom>
									<u-input type="digit" v-model="item.warning" placeholder="请输入二级报警值" border="none">
									</u-input>
								</u-form-item>
								<u-form-item label="三级报警" label-width="75" prop="highRisk" borderBottom>
									<u-input type="digit" v-model="item.highRisk" placeholder="请输入三级报警值" border="none">
									</u-input>
								</u-form-item>
							</u-form>
						</view>
					</uni-card>
				</view>
			</scroll-view>
		</view>
		<view class="button">
			<u-button type="primary" shape='circle'
				color="linear-gradient(to right, rgb(36, 152, 207), rgb(65, 132, 234))" text="保存修改"
				@click="submit"></u-button>
		</view>
		<view class="button">
			<u-button type="success" shape='circle'
				color="linear-gradient(to right, rgb(252, 100, 114), rgb(254, 147, 73))" text="刷新指标"
				@click="syncinfo"></u-button>
		</view>
	</view>
</template>

<script>
	import selectSwitch from "@/components/xuan-switch/xuan-switch.vue";
	export default {
		components: {
			selectSwitch
		},
		data() {
			return {
				mysNavConfig: {
					/* 占位开启  */
					navPadding: true,
					/* 背景 */
					bgColor: "#2979FF",
					/* 标题 （屏幕中心居中 两边图标中心居中使用slot center1） */
					navTitle: {
						text: "报警值配置",
						color: "#ffffff",
						fontSize: "18px", // px upx rpx
						fontWeight: "500", // 100 - 700
					},
					btnType: "type2",
					isCustomFn: true,
					type2Config: {
						// 左图标
						leftPath: "/static/img/back.png",
						// 右图标
						rightPath: "/static/img/home.png",
						// 圆角
						radius: "40rpx",
					}
				},
				equipmentRealId: "",
				pointRealId: '',
				userinfo: '',
				indexinfo: [],
				titlename: '',
				checkedbj: '#18bc37',
				PhoneInfo: '',
				entrust: '',
				urlrouter: '',
			};
		},
		onLoad(parameter) {
			this.PhoneInfo = this.$utils.GetPhoneInfo();
			this.entrust = parameter.entrust;
			this.equipmentRealId = parameter.equipmentRealId;
			this.pointRealId = parameter.pointRealId;
			this.titlename = parameter.name
			this.userinfo = uni.getStorageSync('userinfo');
			this.getindex()
		},
		methods: {
			//跳转到趋势图页面
			pointtrend(item) {
				if (item.code == 'XAN' || item.code == 'YAN' || item.code ==
					'ZAN') {
					var pointTrend = {
						pointRealId: this.pointRealId,
						equipmentRealId: this.equipmentRealId,
						nametitle: this.titlename,
						entrust: this.entrust,
					}
					this.$store.commit('alterpointTrend', pointTrend);
					uni.navigateTo({
						url: '/pages/equipment/pointTrend?id=41'
					})
				} else {
					var pointTrend = {
						pointRealId: this.pointRealId,
						equipmentRealId: this.equipmentRealId,
						nametitle: this.titlename,
						entrust: this.entrust,
						indexid: item.pointFeatureRealId,
					}
					this.$store.commit('alterpointTrend', pointTrend);
					uni.navigateTo({
						url: '/pages/equipment/pointTrend?id=2'
					})
				}
			},
			//全选改变指标状态
			changeallswitch(e) {
				if (e) {
					this.checkedbj = '#18bc37';
					for (let it of this.indexinfo) {
						it.enableStatus = 1
					}
				} else {
					this.checkedbj = '#fa3534';
					for (let it of this.indexinfo) {
						it.enableStatus = 2
					}
				}
			},
			//改变指标状态
			switchchange(value) {
				for (let it of this.indexinfo) {
					if (value == it.pointFeatureRealId) {
						if (it.enableStatus == 1) {
							it.enableStatus = 2
						} else {
							it.enableStatus = 1
						}
					}
				}
			},
			// 返回上一级
			onLeftClick() {
				uni.navigateBack({
					delta: 1
				});
			},
			// 获取指标信息
			getindex() {
				uni.showLoading({
					title: "加载中...",
					mask: true,
				});
				uni.request({
					url: getApp().globalData.serverAddress + this.$uri.Uri_lookPointInfo.replace(
						"{equipmentRealId}", this
						.equipmentRealId).replace("{pointRealId}", this
						.pointRealId).replace("{userRealId}", this.userinfo
						.userRealId),
					method: "GET",
					success: (res) => {
						if (res.statusCode !== 200) {
							this.$utils.showToast(this, '服务器错误,请联系技术支持人员!', 'error');
							return
						}
						if (res.data.code == 0) {
							this.indexinfo = res.data.data.pointFeatures
							for (let item of this.indexinfo) {
								if (item.code == 'POWER' || item.code == 'XAN' || item.code == 'YAN' || item
									.code == 'ZAN') {
									item.yj = item.min.split('&')[0];
									item.bj = item.min.split('&')[1];
									item.gb = item.min.split('&')[2];
								}
							}
						} else {
							this.$utils.showToast(this, res.data.msg, 'error');
						}
					},
					fail() {
						this.$utils.showToast(this, "加载失败！", 'error');
					},
					complete() {
						uni.hideLoading();
					}
				})
			},
			// 修改
			submit() {
				//当entrust是0的时候表示是创建者为1的时候表示授权者
				if (this.entrust == 0) {
					this.urlrouter = this.$uri.Uri_alterTarget;
				} else {
					this.urlrouter = this.$uri.Uri_entrustalterfeature;
				}
				for (let va of this.indexinfo) {
					if (va.unusual === '' || va.warning === '' || va.highRisk === '') {
						this.$utils.showToast(this, va.name + "阈值不能为空", 'warning');
						return;
					}
					if (isNaN(Number(va.unusual)) || isNaN(Number(va.warning)) || isNaN(Number(va.highRisk))) {
						this.$utils.showToast(this, "请输入数字！", 'warning');
						return;
					}
					if (va.code == 'POWER' || va.code == 'XAN' || va.code == 'YAN' || va.code == 'ZAN') {
						if (va.yj === '' || va.bj === '' || va.gb === '') {
							this.$utils.showToast(this, va.name + "阈值不能为空", 'warning');
							return;
						}
						if (isNaN(Number(va.yj)) || isNaN(Number(va.bj)) || isNaN(Number(va.gb))) {
							this.$utils.showToast(this, "请输入数字！", 'warning');
							return;
						}
						if (Number(va.yj) > Number(va.unusual) || Number(va.unusual) > Number(va.bj) || Number(va
								.bj) > Number(va.warning) || Number(va.warning) > Number(va.gb) ||
							Number(va.gb) > Number(va.highRisk)) {
							this.$utils.showToast(this, va.name + "阈值设置错误", 'warning');
							return;
						}
						va.min = va.yj + '&' + va.bj + '&' + va.gb;
					} else {
						if (Number(va.unusual) > Number(va.warning) || Number(va.warning) > Number(va.highRisk)) {
							this.$utils.showToast(this, va.name + "阈值设置错误", 'warning');
							return;
						}
					}
				}
				uni.showLoading({
					title: "修改中...",
					mask: true,
				});
				uni.request({
					url: getApp().globalData.serverAddress + this.urlrouter.replace(
						"{userRealId}", this.userinfo.userRealId).replace(
						"{equipmentRealId}", this.equipmentRealId).replace(
						"{pointRealId}", this.pointRealId),
					data: JSON.stringify(this.indexinfo),
					dataType: "json",
					method: "POST",
					success: (res) => {
						if (res.statusCode !== 200) {
							this.$utils.showToast(this, '服务器错误,请联系技术支持人员!', 'error');
							return
						}
						if (res.data.code == 0) {
							this.$utils.showToast(this, '修改成功！', 'success', 4,
								'/pages/equipmentManage/equipmentList');
						} else {
							this.$utils.showToast(this, res.data.msg, 'error');
						}
					},
					fail() {
						this.$utils.showToast(this, "修改失败！", 'error');
					},
					complete() {
						uni.hideLoading();
					}
				})
			},
			//刷新指标
			syncinfo() {
				uni.showModal({
					confirmColor: '#2979ff',
					cancelText: '否',
					confirmText: '是',
					content: '是否将当前测点的所有指标数据更新为系统内置数据？',
					success: res => {
						if (res.confirm) {
							uni.showLoading({
								title: "更新中...",
								mask: true,
							});
							uni.request({
								url: getApp().globalData.serverAddress + this.$uri.Uri_syncTarget
									.replace(
										"{userRealId}", this.userinfo.userRealId).replace(
										"{equipmentRealId}", this.equipmentRealId).replace(
										"{pointRealId}", this.pointRealId),
								method: "POST",
								success: (res) => {
									if (res.statusCode !== 200) {
										this.$utils.showToast(this, '服务器错误,请联系技术支持人员!', 'error');
										return
									}
									if (res.data.code == 0) {
										this.indexinfo = res.data.data
										for (let item of this.indexinfo) {
											if (item.code == 'POWER' || item.code == 'XAN' || item
												.code == 'YAN' || item
												.code == 'ZAN') {
												item.yj = item.min.split('&')[0];
												item.bj = item.min.split('&')[1];
												item.gb = item.min.split('&')[2];
											}
										}
										this.$utils.showToast(this, '更新成功！', 'success');
									} else {
										this.$utils.showToast(this, res.data.msg, 'error');
									}
								},
								fail: () => {
									this.$utils.showToast(this, '更新失败！', 'error');
								},
								complete: () => {
									uni.hideLoading();
								}
							})
						}
					}
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	.text {
		width: 73%;
		display: inline-block;
	}

	.switch {
		width: 27%;
		display: inline-block;
	}

	.title {
		padding: 0px 10px;
		line-height: 46px;
		height: 46px;
		border-bottom: 0.5px solid #e0e1e2;
	}

	.button {
		margin: 5%;
		width: 40%;
		display: inline-block;
	}

	.uniview {
		margin: 20px;
		padding: 0px 10px;
		background-color: #ffffff;
		box-shadow: 0px 0px 3px 1px rgba(212, 214, 220, 1.0);
		border-radius: 3%;
	}

	.inputviews view {
		display: inline-block;
	}

	.inputview {
		width: 30%;
	}

	.inpvie {
		height: 45px;
		line-height: 45px;
		width: 30%;
		text-align: center;
		font-size: 16px;
	}

	.texts {
		font-size: 20px;
	}
</style>